// pages/readWorks/nonBjObject/index.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    activeIndex: 0,
    navList: [],
    prompt: {
      hidden: !0,
      icon: '../../../assets/images/iconfont-order-default.png',
      title: '无数据',
      text: '查询不到任何学生数据',
      gurl: '../../pages/readWorks/addWork/index',
      gtype: "switchTab"
    },
    liststu: [],
    wxheight: wx.getSystemInfoSync().windowHeight,
    conheight: 400,
    basic_grade_id: '',
    basic_clbum_id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var basic_grade_id = wx.getStorageSync('basic_grade_id')
    var basic_clbum_id = wx.getStorageSync('basic_clbum_id')
    if (basic_grade_id && basic_clbum_id) {
      this.listClbumByValue()
      this.setData({
        conheight: this.data.wxheight - 80
      })
    }else{
      // 只有年级 没有班级
      //搜索班级
      this.listBjByNj()
      this.setData({
        conheight: this.data.wxheight - 130
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  //搜索班级
  listBjByNj(){
    var that = this;
    var nj = wx.getStorageSync('nj')
    app.wxget({
      url: 'issuedWorks/listBjByNj.html?nj=' + nj,
      success: function (res) {
        var navList = res.data
        that.setData({
          navList: navList,
          bj: res.data[0].value,
          basic_grade_id: res.data[0].basic_grade_id,
          basic_clbum_id: res.data[0].id
        })
        that.searchUaccount()
      }
    })
  },


   //搜索年级班级id
   listClbumByValue(){
    var that = this;
    var basic_clbum_id = wx.getStorageSync('basic_clbum_id')
    app.wxget({
      url: 'issuedWorks/listClbumByValue.html?basic_clbum_id='+ basic_clbum_id,
      success: function (res) {
        var navList = res.data
        if (res.data.length > 1) {
          that.setData({
            navList: navList
          })
        }

        that.setData({
          bj: res.data[0].value,
          basic_grade_id: res.data[0].basic_grade_id,
          basic_clbum_id: res.data[0].id
        })
        that.searchUaccount()
      }
    })
  },


  //搜索学生
  searchUaccount(){
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    var bj =  this.data.bj
    var basic_grade_id = this.data.basic_grade_id
    var basic_clbum_id = this.data.basic_clbum_id
    app.wxget({
      url: 'issuedWorks/searchStudent.html',
      data:{
        basic_grade_id: basic_grade_id,
        basic_clbum_id: basic_clbum_id
      },
      success: function (res) {
        var listMap = res.data
        for(var i = 0; i < listMap.length; i++){
          var stumap = listMap[i]
          stumap.chec = false
        }

        var bjmap = {
          bj: bj,
          chec: false,
          listMap: listMap
        }

        // 添加到显示的list中
        var add = true
        var liststu = that.data.liststu
        var editIndex = 0
        for(var j = 0; j < liststu.length; j++){
          if(liststu[j].bj == bj){
            add = false
            editIndex = j
          }
        }
        if (add) {
          liststu.push(bjmap)
        }else{
          liststu.splice(editIndex, 1 ,bjmap)
        }
        that.setData({
          liststu: liststu
        })
        wx.hideLoading()
      }
    })
  },
   //切换
   onTapTag(e) {
      var bj = e.currentTarget.dataset.type;
      var index = e.currentTarget.dataset.index;
      const nav = this.data.navList
      this.setData({
          activeIndex: index,
          bj: bj,
          basic_grade_id: nav[index].basic_grade_id,
          basic_clbum_id: nav[index].id
      })
      var liststu = this.data.liststu
      if (liststu.length < (parseInt(index)+1)) {
        this.searchUaccount()
      }
  },

  // 选择所有
  checkAllChange (e) {
    const bj = e.target.dataset.bj
    const chec = e.target.dataset.chec
    var liststu = this.data.liststu
    for (var k = 0; k < liststu.length; k++) {
      if (liststu[k].bj == bj) {
        liststu[k].chec = !chec
          var stumaps = liststu[k].listMap
          for (var m = 0; m < stumaps.length; m++) {
            stumaps[m].chec = !chec
          }
      }
    }
    this.setData({
      liststu: liststu
    })
  },

  // 选择
  checkItemChange(e) {
      const bj = e.target.dataset.bj
      const usid = e.target.dataset.usid
      const chec = e.target.dataset.chec
      var liststu = this.data.liststu
      for (var k = 0; k < liststu.length; k++) {
        if (liststu[k].bj == bj) {
            var stumaps = liststu[k].listMap
            for (var m = 0; m < stumaps.length; m++) {
              if (stumaps[m].usid == usid) {
                stumaps[m].chec = !chec
              }
            }
        }
      }
      this.setData({
        liststu: liststu
      })
  },

  sureObject () {
    var liststu = this.data.liststu
    var selectmaps = []
    var selectstr= ''
    let nj = wx.getStorageSync('nj')
    for (var k = 0; k<liststu.length; k++) {
      if (liststu[k].chec) {
        var secmap = {}
        secmap.nj = nj
        secmap.bj = liststu[k].bj
        const bjname = nj + '年级' +liststu[k].bj +"班全体学生"
        if (selectstr) {
          selectstr+=","+ bjname
        }else{
          selectstr = bjname
        }
        secmap.usid = ''
        selectmaps.push(secmap)
      }else{
        var stumaps = liststu[k].listMap
        for (var m = 0; m < stumaps.length; m++) {
          if (stumaps[m].chec) {
            var secmap = {}
            secmap.nj = nj
            secmap.bj = liststu[k].bj
            secmap.usid = stumaps[m].usid
            selectmaps.push(secmap)
            if (selectstr) {
              selectstr+=","+stumaps[m].name
            }else{
              selectstr = stumaps[m].name
            }
          }
        }
      }
    }
    this.setData({
      selectmaps: selectmaps
    })
    console.log(selectmaps)
    let pages = getCurrentPages()
    let prePage = pages[pages.length - 2]
    prePage.setData({
      selectObject: selectmaps,
      selectstr: selectstr
    })
    wx.navigateBack({
      delta: 1
    })
  }

})